package com.gaofei.myproject.controller;


import com.gaofei.myproject.mapper.UserMapper;
import com.gaofei.myproject.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/user/login")

    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model, HttpSession session) {

        User user = userMapper.queryUserByName(username);
        //具体的业务
        if (user!=null && user.getPwd().equals(password)){
            session.setAttribute("loginUser", username);
            return "redirect:/main";
        } else {
            model.addAttribute("msg", "用户名或者密码错误");
            return "index";
        }

    }

    //注销
    @RequestMapping("/user/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";

    }

}
